package com.suypower.carManage.json;

import com.jarp.db.DAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.suypower.carManage.comm.ConstUtil;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import java.util.Vector;

/**
 * Created by zhaoyuan on 14-2-18.
 */
public class RoleJson extends ActionSupport{

    private String roleid;
    private String start = "1";
    private String limit = "20";
    private String _dc;

    DAO dao=new DAO();

    /**
     * 用户点击角色名后返回该角色下的所有用户
     * @return
     */
    public String getUserList(){
        StringBuffer sql=new StringBuffer();
        int iStart = Integer.parseInt(start);
        int iPageSize = Integer.parseInt(limit);
        int iPage = iStart / iPageSize + 1;
        sql.append("SELECT t.* FROM tb_user t ");
        sql.append(" LEFT JOIN tb_user_role a ON t.userid=a.userid ");
        sql.append(" LEFT JOIN tb_role b ON a.roleid=b.id ");
        sql.append(" WHERE b.id=").append(roleid);
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME,sql.toString(), iPage, iPageSize);
            if(v!=null){
                String totalCountSQL = "select count(1) CNT from (" + sql + " ) zz ";
                String totalCount = dao.getDataValue(ConstUtil.DBNAMEODS, totalCountSQL);
                JSONArray json = JSONArray.fromObject(v);
                ActionContext.getContext().put("data", "{totalCount:" + totalCount + ",data:" + json.toString() + "}");
            }else{
                ActionContext.getContext().put("data", "{totalCount:0,data:\"\"}");
            }
        } catch (ServletException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 获取权限列表
     * @return
     */
    public String getAccessList(){
        String res="";
        StringBuffer sql=new StringBuffer();
        sql.append("SELECT t.*,b.id AS roleid,a.leavel FROM tb_access t ");
        sql.append("LEFT JOIN (SELECT * FROM tb_role_access where roleid=").append(roleid).append(") a ON t.id=a.accessid ");
        sql.append("LEFT JOIN tb_role  b ON a.roleid=b.id ");
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME,sql.toString());
            if(v!=null){
                JSONArray json = JSONArray.fromObject(v);
                ActionContext.getContext().put("data",json.toString());
            }else{
                ActionContext.getContext().put("data", "\"\"");
            }
        } catch (ServletException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 获取用户列表
     * @return
     */
    public String getAllUsers(){
        StringBuffer sql=new StringBuffer();
        sql.append("SELECT t.*,b.id AS roleid FROM tb_user t ");
        sql.append("LEFT JOIN (SELECT * FROM tb_user_role WHERE roleid="+roleid+") a ON t.userid=a.userid ");
        sql.append("LEFT JOIN tb_role b ON a.roleid=b.id ");
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME,sql.toString());
            if(v!=null){
                JSONArray json = JSONArray.fromObject(v);
                ActionContext.getContext().put("data",json.toString());
            }else{
                ActionContext.getContext().put("data", "\"\"");
            }
        } catch (ServletException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    public String getRoleid() {
        return roleid;
    }

    public void setRoleid(String roleid) {
        this.roleid = roleid;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }

    public String get_dc() {
        return _dc;
    }

    public void set_dc(String _dc) {
        this._dc = _dc;
    }


}
